<?php

namespace app\modules\api\controllers;
use Yii;
use yii\wxcrypt\WXBizDataCrypt;
use app\modules\api\CommonController;
require_once(Yii::$app->basePath.'/vendor/wxcrypt/WXBizDataCrypt.php');
require_once(Yii::$app->basePath.'/vendor/wxcrypt/ErrorCode.php');

/**
 * Default controller for the `api` module
 */
class AuthController extends CommonController
{
    //微信解密
    public function actionWx()
    {
        try{
            $encryptedData = Yii::$app->request->post('encryptedData','');
            $errMsg = Yii::$app->request->post('errMsg','');
            $iv = Yii::$app->request->post('iv','');
            $err_arr = explode(':',$errMsg);
            if($err_arr[1]!='ok') throw new \Exception('获取加密消息失败',1);
            $pc = new WXBizDataCrypt('wx945baaf3dfb83a32', 'vZdc+Sll5w0sVPjXKiQbwA==');
            $errCode = $pc->decryptData($encryptedData, $iv, $data );
            if ($errCode != 0) throw new \Exception('微信解密错误:'.$errCode,1);
            $data = json_decode($data,1);

            return $this->success($data);
        }catch (\Exception $e){
            return $this->error($e);
        }
    }

}
